package net.wofly.right.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.wofly.common.util.ResultCode;
import net.wofly.common.util.ReturnBean;
import net.wofly.right.domain.FuncList;
import net.wofly.right.domain.RightConfig;
import net.wofly.right.domain.Role;
import net.wofly.right.dto.MenuDto;
import net.wofly.right.repository.RightConfigRepository;
import net.wofly.right.service.IRightConfigService;

@Service("iRightConfigService")
@Transactional
public class RightConfigServiceImpl implements IRightConfigService {

    @Autowired
    RightConfigRepository rightConfigRepository;

    @Override
    public RightConfig save(RightConfig entity) {
        return rightConfigRepository.save(entity);
    }

    @Override
    public void delete(RightConfig entity) {
        rightConfigRepository.delete(entity);
    }

    @Override
    public void delete(String key) {
        rightConfigRepository.delete(key);
    }

    @Override
    public RightConfig findOne(String key) {
        return rightConfigRepository.findOne(key);
    }

    @Override
    public List<RightConfig> save(List<RightConfig> entityList) {
        return (List<RightConfig>) rightConfigRepository.save(entityList);
    }

    @Override
    public List<Role> findRolesByFuncListID(String funcListID) {
        return rightConfigRepository.findRolesByFuncListID(funcListID);
    }

    @Override
    public List<FuncList> findFuncListByRoleID(String roleID) {
        return rightConfigRepository.findFuncListByRoleID(roleID);
    }

    @Override
    public ReturnBean<List<FuncList>> getRightConfigByRoleID(String roleID) {
        List<FuncList> funcLists = rightConfigRepository.findFuncListByRoleID(roleID);

        if (funcLists != null) {
            List<FuncList> parentList = new ArrayList<>();
            for (FuncList funcList : funcLists) {
                String funcCode = funcList.getFuncCode();
                if (funcCode.length() == 3) {
                    parentList.add(funcList);
                } else {
                    for (FuncList parentFuncList : parentList) {
                        if (funcCode.startsWith(parentFuncList.getFuncCode())) {
                            List<FuncList> funcListList = parentFuncList.getFuncListList();
                            if (funcListList == null) {
                                funcListList = new ArrayList<>();
                            }
                            funcListList.add(funcList);
                            parentFuncList.setFuncListList(funcListList);
                            break;
                        }
                    }
                }
            }
            return new ReturnBean<>(ResultCode.SUCCESS, "获取客户列表成功！", parentList) ;
        }
        return new ReturnBean<>(-2, "未获取到功能列表！") ;
    }

	@Override
	public List<MenuDto> getRightConfigByRoleIDForLogin(String roleID) {
		String icon = "sm-iconfont icon-sm-shezhi-xianxing" ;
		
		List<FuncList> funcLists = rightConfigRepository.findFuncListByRoleID(roleID);
		List<MenuDto> menuDtos = new ArrayList<>();
		
		if (funcLists == null || funcLists.isEmpty()) {
			return menuDtos ;
		}
		 
		 List<FuncList> parentList = new ArrayList<>();
         for (FuncList funcList : funcLists) {
             String funcCode = funcList.getFuncCode();
             if (funcCode.length() == 3) {
                 parentList.add(funcList);
             } else {
                 for (FuncList parentFuncList : parentList) {
                     if (funcCode.startsWith(parentFuncList.getFuncCode())) {
                         List<FuncList> funcListList = parentFuncList.getFuncListList();
                         if (funcListList == null) {
                             funcListList = new ArrayList<>();
                         }
                         funcListList.add(funcList);
                         parentFuncList.setFuncListList(funcListList);
                         break;
                     }
                 }
             }
         }
         
         MenuDto menuDto ;
         MenuDto subMenuDto ;
         List<MenuDto> subMenuDtos  ;
         for(FuncList funcList:parentList) {
        	 	menuDto = new MenuDto() ;
        	 	menuDto.setIcon(icon);
        	 	menuDto.setName(funcList.getFuncName());
        	 	menuDto.setTitle(funcList.getFuncName());
        	 	menuDto.setToPath(funcList.getFuncUrl());
        	 	menuDto.setIsShow(1);
        	 	List<FuncList> sublist = funcList.getFuncListList() ;
        	 	if(null==sublist || sublist.isEmpty()) {
        	 		menuDtos.add(menuDto) ;
        	 		continue ;
        	 	}
        	 	
        	 	subMenuDtos = new ArrayList<>() ;
        	 	for(FuncList subFuncList:sublist) {
        	 		subMenuDto = new MenuDto() ; 
        	 		subMenuDto.setIcon(icon);
        	 		subMenuDto.setName(subFuncList.getFuncName());
        	 		subMenuDto.setTitle(subFuncList.getFuncName());
        	 		subMenuDto.setToPath(subFuncList.getFuncUrl());
        	 		subMenuDto.setIsShow(1);
        	 		subMenuDtos.add(subMenuDto) ;
        	 	}
        	 	
        	 	menuDto.setSubList(subMenuDtos);
        	 	
        	 	menuDtos.add(menuDto) ;
         }
         
         return menuDtos ;
	}

    @Override
    public List<RightConfig> findByRoleID(String roleID) {
        return rightConfigRepository.findByRoleID(roleID);
    }

    @Override
    public List<RightConfig> findByRoleIDAndFuncListID(String roleID, String funcListID) {
        return rightConfigRepository.findByRoleIDAndFuncListID(roleID, funcListID);
    }
}
